package shop;
//201621123079 刘思远
public class CartArrayImpl extends Mart implements CartDao{
	
	class Cart{
		Goods goods;
		int num;
	}
	
	Cart []cart = new Cart[100];
	double allPrice = 0;
	int top = -1;
	
	public void addGoodsInCar(int i,int num) {
		top++;
		cart[top] = new Cart();
		cart[top].goods = getOne(i);
		cart[top].num = num;
		allPrice += (num*getOne(i).getPrice());
	}
	
	public void removeGoods(int i) {
		Goods g = getOne(i);
		for (int j = 0; j < top+1; j++) {
			if(cart[j].goods==g) {
				if(j == top) {
					allPrice -= (cart[top].num*cart[top].goods.getPrice());
					top--;
				}
				else {
					allPrice -= (cart[j].num*cart[j].goods.getPrice());
					for (int j2 = j; j2 < top; j2++) {
						cart[j2] = cart[j2+1];
						top--;
					}
				}
				break;
			}
		}
	}
	
	public void ShowCar() {
		System.out.println("购物车:");
		for (int i = 0; i < top+1; i++) {
			System.out.println(cart[i].goods.toString() + " 数量: " + cart[i].num);
		}
		System.out.println("总价: " + allPrice);
	}
	
        public String ShowCarToGui(){
            String str = "";
            boolean f = false;
            if(top == -1) return "购物车空空如也";
            for (int i = 0; i < top+1; i++) {
			if(!f) str = str + cart[i].goods.toString() + " 数量: " + cart[i].num;
                        else str = str + "<br>" + cart[i].goods.toString() + " 数量: " + cart[i].num;
                        f = true;
		}
            return "<html><body>"+str+"</body></html>";
        }
        
        public String ShowCarToGuiAllPrice(){
            return "" + allPrice;
        }
        
	public void Pay() {
		top = -1;
		allPrice = 0;
	}
}
